package com.jokul.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/index.do")
public class Index extends HttpServlet {

	private static final long serialVersionUID = 1983663539844674117L;
	
	private void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		System.out.println(session.getAttribute("username"));
		Cookie[] cookies = request.getCookies();
		if(cookies != null) {
			for(Cookie cookie:cookies) {
				String name = cookie.getName();
				String value = cookie.getValue();
				if("user".equals(name) && "caterpillar".equals(value)) {
					request.setAttribute(name, value);
					request.getRequestDispatcher("member.jsp").forward(request, response);
					return;
				}
			}
		}
		response.sendRedirect("/login.do");
	}
	
	protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}
	
	protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

}
